package com.cloud.wms.ui.sort;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.sort.contract.BatchContract;
import com.cloud.wms.ui.sort.entity.BatchDetailEntity;
import com.cloud.wms.ui.sort.presenter.BatchPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author:Chen
 * @Date: 2021-01-19 16:45
 * @Description:
 */
public class BatchActivity extends BaseBarListActivity<CommodityEntity, BatchPresenter> implements BatchContract.View {

    @BindView(R.id.tv_batch_code)
    TextView tvBatchCode;
    @BindView(R.id.tv_batch_type)
    TextView tvBatchType;
    @BindView(R.id.tv_batch_sku)
    TextView tvBatchSku;
    @BindView(R.id.tv_batch_amount)
    TextView tvBatchAmount;
    @BindView(R.id.tv_batch_create)
    TextView tvBatchCreate;
    @BindView(R.id.tv_sort_progress)
    TextView tvSortProgress;
    private TaskEntity taskEntity;

    @Override
    protected String barTitle() {
        return "查看详情";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_batch_detail;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_batch_detail;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        taskEntity = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);

        mPresenter.loadBatchDetail(taskEntity);
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadBatchDetail(taskEntity);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, CommodityEntity item) {
        helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_commodity_code, item.getBarCode())
                .setText(R.id.tv_commodity_amount_name, "商品数量")
                .setText(R.id.tv_sort_amount, item.getAmount() + "")
                .setText(R.id.tv_commodity_type, item.getStockTypeName().replace("品", ""))
                .setBackgroundResource(R.id.tv_commodity_type, item.getStockType() == 1 ? R.drawable.shape_in_detail_good : R.drawable.shape_in_detail_bad);

    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        if (message.what == BatchPresenter.BATCH_DETAIL) {
            BatchDetailEntity batchDetailEntity = (BatchDetailEntity) message.obj;
            mAdapter.setNewInstance(batchDetailEntity.getCommodityEntities());

            tvSortProgress.setText(batchDetailEntity.getInfoEntity().getSortProgress());
            tvBatchAmount.setText(batchDetailEntity.getInfoEntity().getTotalAmount());
            tvBatchCode.setText(batchDetailEntity.getInfoEntity().getBatchCode());
            tvBatchCreate.setText(batchDetailEntity.getInfoEntity().getCreateDate());
            tvBatchType.setText(batchDetailEntity.getInfoEntity().getBatchTypeName());
            tvBatchSku.setText(batchDetailEntity.getInfoEntity().getSkuAmount());
        }
    }

    public static void start(Context context, TaskEntity entity) {
        Intent intent = new Intent(context, BatchActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, entity);
        context.startActivity(intent);
    }
}
